package com.mobilepay.moneypig.model.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.home.NoteDetailData;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;

/**
 * Created by xlm on 2017/7/26 0026
 */

public class NoteDetailaActivity extends BaseActivity {
    private RelativeLayout mRelativeBack;
    private TextView mTextType, mTextTime, mTextContent;
    private int id, uid;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_notedetail);
    }

    @Override
    protected void onGetIntent(Intent intent) {
        super.onGetIntent(intent);
        id = intent.getIntExtra("id", 0);
        uid = intent.getIntExtra("uid", 0);
    }

    private void getDetailData() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.detail);
        param.add("uid", String.valueOf(uid));
        param.add("id", String.valueOf(id));
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 1) {
                        final NoteDetailData data = GsonUtil.GsonToBean(string, NoteDetailData.class);
                        if (data == null) {
                            return;
                        }
                        final NoteDetailData.DataBean dataBean = data.getData();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mTextType.setText(dataBean.getTitle());
                                mTextContent.setText(dataBean.getBody());
                                mTextTime.setText(CommonUtil.formatDate(dataBean.getCreate_time() * 1000, "yyyy-MM-dd HH:mm:ss"));
                            }
                        });
                    } else {
                        showToastShort(responseResult.getInfo());
                    }
                }
            }
        });
    }

    @Override
    protected void initViews() {
        mRelativeBack = (RelativeLayout) findViewById(R.id.detail_back);
        mTextType = (TextView) findViewById(R.id.detail_type);
        mTextTime = (TextView) findViewById(R.id.note_time);
        mTextContent = (TextView) findViewById(R.id.note_content);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        getDetailData();
    }

    @Override
    protected void onSetListener() {
        mRelativeBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
